package com.yzt.zhuju.utils;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.kongzue.dialog.v3.CustomDialog;
import com.yzt.zhuju.R;
import com.yzt.zhuju.config.AppConfig;

/**
 * desc   : 弹窗处理工具
 */
public class AppDialogUtil {

    public static void showNoTitleDialog(Activity mActivity, Object content, DialogClickInterface mDialogClickInterface) {

        CustomDialog.show((FragmentActivity) mActivity, R.layout.dialog_no_title_default, new CustomDialog.OnBindView() {
            @Override
            public void onBind(CustomDialog dialog, View rootView) {
                TextView tvContent = rootView.findViewById(R.id.tv_content);
                tvContent.setText(getString(content));
                TextView tvCancel = rootView.findViewById(R.id.tv_cancel);
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                        mDialogClickInterface.onCancelClick();
                    }
                });
                TextView tvSure = rootView.findViewById(R.id.tv_sure);
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                        mDialogClickInterface.onSureClick();
                    }
                });
            }
        });
    }

    /**
     * showNoTitleDialog
     *
     * @param content        显示内容
     * @param cancelContent  取消文案
     * @param confirmContent 确认文案
     */
    public static void showNoTitleDialog(Activity mActivity, Object content,
                                         Object cancelContent,
                                         Object confirmContent,
                                         DialogClickInterface mDialogClickInterface) {
        CustomDialog.show((FragmentActivity) mActivity, R.layout.dialog_no_title_default, new CustomDialog.OnBindView() {
            @Override
            public void onBind(CustomDialog dialog, View rootView) {
                TextView tvContent = rootView.findViewById(R.id.tv_content);
                tvContent.setText(getString(content));
                TextView cancel = rootView.findViewById(R.id.tv_cancel);
                cancel.setText(getString(cancelContent));
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                        mDialogClickInterface.onCancelClick();
                    }
                });
                TextView confirm = rootView.findViewById(R.id.tv_sure);
                confirm.setText(getString(confirmContent));
                confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                        mDialogClickInterface.onSureClick();
                    }
                });
            }
        });
    }

    /**
     * showTitleDialog
     */
    public static void showTitleDialog(Activity mActivity, Object title, Object content, Object cancelContent, Object positiveContent, DialogClickInterface mDialogClickInterface) {

        CustomDialog.show((FragmentActivity) mActivity, R.layout.dialog_has_title_default, new CustomDialog.OnBindView() {
            @Override
            public void onBind(CustomDialog dialog, View rootView) {
                TextView tvTitle = rootView.findViewById(R.id.tv_title);
                tvTitle.setText(getString(title));
                TextView tvContent = rootView.findViewById(R.id.tv_content);
                tvContent.setText(getString(content));
                TextView tvCancel = rootView.findViewById(R.id.tv_cancel);
                tvCancel.setText(getString(cancelContent));
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                        mDialogClickInterface.onCancelClick();
                    }
                });
                TextView tvSure = rootView.findViewById(R.id.tv_sure);
                tvSure.setText(getString(positiveContent));
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.doDismiss();
                        mDialogClickInterface.onSureClick();
                    }
                });
            }
        });
    }

    private static String getString(Object object) {
        if (object instanceof String) {
            return (String) object;
        } else if (object instanceof Integer) {
            return AppConfig.application.getString((Integer) object);
        } else {
            return null;
        }
    }

    public interface DialogClickInterface {

        void onCancelClick();

        void onSureClick();
    }
}
